<template>
  <div>
    <el-table :data="userData" border style="width: 100%">
      <el-table-column fixed prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="sex" label="性别" :formatter="sexFormatter">
      </el-table-column>
      <el-table-column prop="age" label="年龄"> </el-table-column>
      <el-table-column prop="birthday" label="生日"> </el-table-column>
      <el-table-column prop="creatTime" label="创建时间"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="insertUser()">新增</el-button
          >
          <el-button @click="deleteUser(scope.row.id)" type="text" size="small">删除</el-button>
          <el-button type="text" size="small" @click="UpdateUser(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @next-click="queryNextPage"
      @prev-click="queryPrePage"
      @size-change="sizeChange"
      @current-change="currentChange"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      background
      layout="prev, pager, next,sizes,jumper, total"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    insertUser(){
        this.$router.push("/toAddUser");
    },
    deleteUser(id) {
      var app = this;
      this.$httpRequest
        .get("user/deleteById/" + id, {})
        .then(function () {
          app.$router.push("/toUserList");
        })
        .catch(function (error) {
          //失败后调用
        })
        .then(function () {
          //不论成功或者失败后都调用
        });
    },
    sexFormatter(row, column, cellValue, index) {
      return cellValue == 1 ? "男" : "女";
    },
    queryNextPage(page) {
      this.current = page;
      this.getUserPage();
    },
    queryPrePage(page) {
      this.current = page;
      this.getUserPage();
    },
    sizeChange(page) {
      this.pageSize = page;
      this.getUserPage();
    },
    currentChange(page) {
      this.current = page;
      this.getUserPage();
    },
    getUserPage() {
      var app = this;
      this.$httpRequest
        .post("user/page", {
          current: app.current,
          size: app.pageSize,
        })
        .then(function ({ data }) {
          //成功后调用
          console.log("成功", data);
          app.userData = data.page.records;
          app.current = data.page.current;
          app.size = data.page.size;
          app.total = data.page.total;
        })
        .catch(function (error) {
          //失败后调用
        })
        .then(function () {
          //不论成功或者失败后都调用
        });
    },
    UpdateUser(id){
      this.$router.push("/toAddUser/"+id);
    }
  },

  data() {
    return {
      total: 20, //总页数
      pageSize: 5, //每页数量
      pageSizes: [5, 10, 20],
      current: 1, //当前页
      userData: [
        {
          username: "",
          sex: "",
          age: "",
          birthday: "",
          creatTime: "",
        },
      ],
    };
  },
  mounted: function () {
    //页面加载时调用
    this.getUserPage();
  },
};
</script>

<style>
</style>